<?php
/**
 * Created by PhpStorm.
 * User: 户学
 * Date: 2018/4/23
 * Time: 16:37
 */

class Sign extends M_Controller
{
    public function index(){
        if($_POST){
            $name = $this->input->get_post('name');
            $class = $this->input->get_post('class');
            $subject = $this->input->get_post('project');
            $phone = $this->input->get_post("mobile");
            if(!name){
                $error = array('name' => 'code', 'msg' => fc_lang('姓名不能为空'));
            }
            if(!$class){
                $error = array('name' => 'code', 'msg' => fc_lang('年级不能为空'));
            }
            if(!$subject){
                $error = array('name' => 'code', 'msg' => fc_lang('科目不能为空'));
            }
            if(!$phone){
                $error = array('name' => 'code', 'msg' => fc_lang('手机号码不能为空'));
            }
            $data = array("name"=>$name,"grade"=>$class,"subject"=>$subject,"mobile"=>$phone,"remark"=>'免费试听',"addtime"=>date('Y-m-d H:i:s',time()));
            $id = $this->db->insert("try_apply", $data);
            if($id){
               $memo = ACTIVITY_TYPE_2;
               $ecArr = array("f_name"=>$name,"f_mobile"=>$phone,"81157087"=> $class,"81157089"=>$subject,"optUserId"=>"7173319","f_memo"=> $memo,"81157096"=>'免费试听',
                    "customFieldMapping"=>array(
                        "81157087"=>array("option"=>'',"type"=>"1"),
                        "81157089"=>array("option"=>'',"type"=>'1'),
                        "81157096"=>array("option"=>'',"type"=>'4')
                    ));
                $apps = array("appId"=>APP_ID, "appSecret"=>APP_SECRET);
                $arrs  = ec_getToken(EC_GET_TOKEN,json_encode($apps));
                $tok = json_decode($arrs,true);
                $token = $tok['data']['accessToken'];
                ec_getData(EC_REGISTER_URL, json_encode($ecArr),array("Authorization:$token", "CORP_ID:".CORP_ID));
                 exit($this->callback_json(array(
                    'msg' => '报名成功',
                    'code' => 1,
                    'uid' => $id
                )));
            }

            exit($this->callback_json(array(
                'msg' => $error['msg'],
                'code' => 0
            )));
            if (IS_AJAX) {
                $error && exit(dr_json(0, $error['msg']));
                $id > 0 && exit(json_encode(array(
                    'status' => 1,
                    'backurl' => $back_url,
                    'syncurl' => dr_member_sync_url($code))));
            }
            $code && $this->member_msg(fc_lang('报名成功').$code, $back_url, 3, 6);
            exit;

        }
        $this->template->display("../gw/sign.html");

    }

}